package cn.cmdr.service.ServiceImpl;

import cn.cmdr.mapper.ComMapper;
import cn.cmdr.mapper.StockMapper;
import cn.cmdr.pojo.Commodity;
import cn.cmdr.pojo.Stock;
import cn.cmdr.service.ComService;
import cn.cmdr.vo.ComVo;
import cn.cmdr.vo.PageVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("comService")
public class ComServiceImpl extends ServiceImpl<ComMapper, Commodity> implements ComService {
    @Resource
    private ComMapper comMapper;
    @Resource
    private StockMapper stockMapper;

    @Override
    public List<Commodity> getComList(Integer id) {
        return comMapper.getComList(id);
    }

    @Override
    public PageVo getPageComList(Integer pageNo, Integer pageSize, ComVo comVo) {
        Page page = new Page();
        page.setSize(pageSize);
        page.setCurrent(pageNo);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like(!StringUtils.isEmpty(comVo.getName()),"name",comVo.getName())
                .like(comVo.getProductSn()!=null,"product_sn", comVo.getProductSn())
                .eq(comVo.getProductCategoryId()!=null," product_category_id", comVo.getProductCategoryId())
                .eq(comVo.getBrandId()!=null,"brand_Id",comVo.getBrandId())
                .eq(comVo.getPublishStatus()!=null,"publish_status",comVo.getPublishStatus())
                .eq(comVo.getVerifyStatus()!=null,"verify_status",comVo.getVerifyStatus())
                .eq(comVo.getVerifyStatus()!=null,"verify_status",comVo.getVerifyStatus())
                .eq(true,"delete_status",0)
                .orderByDesc("c.sort");
        List<Commodity> list = comMapper.getComPageList(page,queryWrapper);
        return new PageVo(list,page.getTotal());
    }
    @Override
    public List<Stock> getStockByComId(Integer commodityId){
        List<Stock> list = comMapper.getStockByComId(commodityId);
        return list;
    }
    @Override
    public Commodity getComById(Integer id){
        Commodity commodity = comMapper.getComById(id);
        return commodity;
    }

    @Override
    public void stockUpdate(Stock stock){ stockMapper.updateById(stock); }


    @Override
    public void comUpdate(Commodity commodity){ comMapper.updateById(commodity); }
    @Override
    public void add(Commodity commodity){ comMapper.insert(commodity); }
    @Override
    public void sAdd(Stock stock){ stockMapper.insert(stock); }
    @Override
    public void delete(Integer id){ comMapper.deleteById(id); }
    @Override
    public int getCount(Integer publishStatus) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(publishStatus!=null,"publish_status",publishStatus)
                .eq(true,"delete_status",0);
        int count = super.count(queryWrapper);
        return count;
    }

    @Override
    public int getKCount() {
        return comMapper.getKCount();
    }

    @Override
    public int getVCount(Integer verifyStatus) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(verifyStatus!=null,"verify_status",verifyStatus)
                .eq(true,"delete_status",0);
        int c = super.count(queryWrapper);
        return c;
    }

    @Override
    public Commodity getCom(Integer id) {
        return comMapper.getCom(id);
    }

    @Override
    public void update(Commodity commodity) {
        comMapper.updateById(commodity);
    }

    @Override
    public List<Stock> getSk(Stock stock) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(stock.getCommodityId() != null, "commodity_id", stock.getCommodityId())
                .like(stock.getStockCode()!=null,"stock_code",stock.getStockCode());
        return stockMapper.selectList(queryWrapper);
    }

    @Override
    public List<Commodity> getcBYyId(Integer brandId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(brandId!=null,"brand_id",brandId)
                .eq(true,"delete_status",0);
        return comMapper.selectList(queryWrapper);
    }

    public Commodity getc(Commodity commodity){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(!StringUtils.isEmpty(commodity.getProductSn()),"product_sn",commodity.getProductSn());
        return comMapper.selectOne(queryWrapper);
    }

    @Override
    public Stock getst(Integer id) {
        return stockMapper.selectById(id);
    }

    @Override
    public List<Commodity> getsById(Integer productCategoryId){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(productCategoryId!=null,"product_category_id",productCategoryId)
                .eq(true,"delete_status",0);
        return comMapper.selectList(queryWrapper);
    }

    @Override
    public void delestock(Integer id) {
        stockMapper.deleteById(id);
    }
}
